"""
@name : 8.11ip re作业
@author : chenlu
@projectname: 联合国数据爬取.py
"""

import IPy
import time
from pythonping import ping
from multiprocessing import Process
def ip_text(ip):
    res = ping(ip, timeout = 1)
    time.sleep(2)
    with open("net.txt", 'a+') as fp:
        if res.success():
            print(str(ip)+':'+ "up")
            fp.write(str(ip)+':'+"up\n")
        else:
            print(str(ip)+":"+"down")
            fp.write(str(ip)+':'+'down\n')

ip_net = '192.168.0.0/24'
ips = IPy.IP(ip_net)
p_lst = []

if __name__ == "__main__":
    for ip in ips:
        p = Process(target=ip_text, args=(str(ip),))
        p_lst.append(p)
        p.start()
        [p.join() for p in p_lst]


import requests
import re

url = "https://www.sanchuangedu.cn/"
r = requests.get(url)
r.encoding = "utf8"
rtext = r.text
img_list = re.findall(r"(?<=<img src=).*[.jpg|.png]", rtext)

for addr in img_list:
    src = 'https://www.sanchuangedu.cn/' + addr
    imgdata = requests.get(src).content
    imgpath = 'D:\\sc\\python代码文件\\img\\'+addr
    with open(imgpath, 'wb') as fp:
        fp.write(imgdata)
        print(f'{addr} 下载成功！')
